﻿--[[
   @brief    frame_bottom logic for  mianframe right bottom
   @author   harleyxwang
   @version  1.0
   @date     2016-09-13
--]]

frame_bottom_model =
{
    PLUGIN_NOT_EXIST_ERRCODE = -2;
    def_module_color = "COLOR:RGB:255,255,255";      -- Native底栏背景颜色
    
    plugin_setting_info = nil;

    plugin_more_setting_info = {};

    gift_download_switch_tbl = {};

    GAME_TYPE = {
        MMOG_GAME       = 0,
        PHONE_GMAE      = 1,
        WEB_GAME        = 2,
        STANDALONG_GAME = 3,
        OTHER = 4
    };

    is_init_gray_switch = false;
    is_open_feature = true;

    cur_lol_server_id = 0;

    is_user_protocol_agree_table = nil;

    is_game_base_info_nil = false;

    cur_switch_module_id = nil; -- 保存当前模块的id
    wechat_bind_qq_item = "26";
    need_show_wechat_bind_qq_wnd = false;
    has_get_wechat_bind_flag = false;
    lol_bind_query_url = LoadStr("LOL_WECHAT_BIND_QUERY_URL");
    lol_game_id = 0;
    lol_service_id = 0;
    cur_game_ctrl_id_2_prtcl_info = {};

    -- 显示礼包
    download_gift_data = {};     -- key(int): game_id, value: {status:状态,url:链接}
                                    -- status状态：0->不显示 1->要显示 -1->已显示
  
    show_new_frame_bottom_ = false;   -- 是否展示新底栏样式

    init = function()
        local tpfui = base_utility.get_component_adapter("ITpf_ui",
                       "ierd_tgp.tpf_ui.GetTPFUI();");
        tpfui:RegisterJsEvent("m_get_branch_info", "frame_bottom_model.handel_js_branch_operation");
        tpfui:RegisterJsEvent("m_switch_branch", "frame_bottom_model.handel_js_branch_operation");
        comm_center.listen_broadcast(
                     "i_broadcast_user_setting_update", frame_bottom_model.on_user_setting_update);
    end;

    get_plugin_setting_info = function (game_id)
        -- 统一使用banner中的数据
        local banner_info = banner_model.get_banner_module_info_by_game_id(game_id);

        if nil == banner_info then
            return nil;
        end

        return banner_info.plugin_setting;
    end;
   
    get_helper_simple_area_setting = function(game_id)
        if game_id == nil or banner_model == nil then
            return nil;
        end
        local banner_info = banner_model.get_banner_module_info_by_game_id(game_id);
        if nil == banner_info then
            return nil;
        end
        return banner_info.helper_simple_area_setting;
    end;

    -- 显示超级多开按钮
    is_super_multi_account = function(game_id)
        --[[
        local helper_simple_setting = frame_bottom_model.get_helper_simple_area_setting(game_id);
        if helper_simple_setting ~= nil and helper_simple_setting ~= "" then
            local setting_tbl = base_utility.json_to_table(helper_simple_setting);
            if setting_tbl ~= nil and setting_tbl.custom_setting ~= nil then 
                if setting_tbl.custom_setting[1] ~= nil and setting_tbl.custom_setting[1].type == "button" then 
                    if setting_tbl.custom_setting[1].name == "super_multi_account" then
                        return true;
                    end
                end
            end
        end
        return false
        ]]
        return true;
    end;

    -- 获取native底栏的灰度开关控制 
    is_open_bottom_feature = function()
        local tpfui =
                base_utility.get_component_adapter("ITpf_ui",
                    "ierd_tgp.tpf_ui.GetTPFUI();");
   
        local is_english_ver = tpfui:Is_English_ver();

        if frame_bottom_model.is_init_gray_switch then
            if is_english_ver == true then
                frame_bottom_model.is_open_feature = true;
            end

            return frame_bottom_model.is_open_feature;
        end

        local feature_mgr =
            base_utility.get_component_adapter("IFeature_mgr",
                "ierd_tgp.feature.GetFeatureMgrAdapter();");
        if feature_mgr ~= nil then
            frame_bottom_model.is_open_feature = feature_mgr:support_feature(ierd_tgp.feature.bottom_toolbar);
            frame_bottom_model.is_init_gray_switch = true;
        end
        base_utility.log(string.format(
            "[frame_bottom]open bottom feature: %s", tostring(frame_bottom_model.is_open_feature)), true);

        if is_english_ver == true then
            frame_bottom_model.is_open_feature = true;
        end

        return frame_bottom_model.is_open_feature;
    end;

    get_all_branch_infos = function(game_id)
        base_utility.log("[frame_bottom]get_all_branch_infos");

        local game_lib = base_utility.get_component_adapter("IGame_library",
              "ierd_tgp.game_library.Get_game_library_adapter();");
        if game_lib == NULL or game_id == nil or game_id == 0 then
            base_utility.log("[frame_bottom]get_all_branch_infos nil");
            local infos = {game_id = game_id, count = 0, cur_branch_id = nil, info = {}};
            return infos;
        end
        --[[ 自测数据 
        local test_infos = {game_id=game_id, count = 4, cur_branch_id = 1, info={}};
        table.insert(test_infos.info,
          {branch_id = 1,
                app_name = "分支1",
                status = 1,
                token = nil});
        table.insert(test_infos.info,
          {branch_id = 2,
                app_name = "分支2",
                status = 1,
                token = "123"});
        table.insert(test_infos.info,
              {branch_id = 3,
                app_name = "分支3",
                status = 1,
                token = nil});
        table.insert(test_infos.info,
              {branch_id = 4,
                app_name = "分支4",
                status = 1,
                token = nil});
                   
        return test_infos;
        --]] 

        local branch_info = ierd_tgp.game_library.AllBranchInfo:new();
        game_lib:GetAllBranchInfo(game_id, branch_info);
        local infos = {game_id = game_id, 
          count = branch_info.count, 
          cur_branch_id = branch_info.cur_branch_id,
          info = {}};
        -- C结构从0开始.
        for i = 0, infos.count - 1 do
            table.insert(infos.info,
              {branch_id = branch_info.info[i].branch_id,
                app_name = branch_info.info[i].app_name,
                status = branch_info.info[i].status,
                token = branch_info.info[i].token,
                privilege = branch_info.info[i].has_privilege});
        end
        branch_info:delete();
        return infos;
    end;
    
    get_branch_info = function(game_id, branch_id)
        local infos = frame_bottom_model.get_all_branch_infos(game_id);
        -- lua结构从1开始
        local info = nil;
        for i = 1, infos.count do
            if infos.info[i].branch_id == branch_id then
              infos.info[i].game_id = game_id;
              info = infos.info[i];
              info.cur_branch_id = infos.cur_branch_id;
              return info;
            end;
        end
        return nil;
    end;
    
    switch_branch = function (game_id, branch_id)
        if type(branch_id) == "string" then
          branch_id = tonumber(branch_id)
        end
        if type(game_id) == "string" then
          game_id = tonumber(game_id)
        end
        if game_id == nil or game_id == 0 or branch_id == nil then
          base_utility.log("[frame_bottom]switch_branch nil");
          return
        end
        
        local game_lib =
              base_utility.get_component_adapter("IGame_library",
                  "ierd_tgp.game_library.Get_game_library_adapter();");
          if  game_lib ~= NULL then
              -- check
              local infos = ierd_tgp.game_library.AllBranchInfo:new_local();
              game_lib:GetAllBranchInfo(game_id,infos);
              if infos.cur_branch_id == branch_id then
                base_utility.log("[frame_bottom]switch_branch same branch_id");
                return 
              end

              local found = false
              for i=0,infos.count-1 do
                  if infos.info[i].branch_id == branch_id then
                      if infos.info[i].status < 0 then
                        base_utility.log("[frame_bottom]switch_branch branch removed");
                        return
                      end
                      found = true
                      break
                  end
              end
              
              if found == true then
                -- 切换分支
                base_utility.log("[frame_bottom]switch_branch to ".. branch_id);
                game_lib:SwitchBranch(game_id, branch_id);
              end
          end;
    end;

    -- web tgp.get cmd:"m_get_branch_info". 返回游戏分支信息
    handel_js_branch_operation = function(func_name, json, browser_id)
      base_utility.log("[frame_bottom]handel_js_branch_operation func:".. func_name);
      if func_name == "m_get_branch_info" then
          local param = base_utility.json_to_table(json);
          if nil ~= param then
              local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
          
              if param.callback_func ~= nil and param.callback_func ~= "" then
                  rsp_table.data = frame_bottom_model.get_all_branch_infos(param.data.game_id);
                  local rsp_json = base_utility.table_to_json(rsp_table);
                  web_utility.lua_call_js_by_id_ex(param.callback_func, rsp_json, browser_id);
              end
          end
          return
      end
      if func_name == "m_switch_branch" then
        local param = base_utility.json_to_table(json);
          if nil ~= param then
              local rsp_table = web_utility.web_rsp_comm_data(param, 0, "");
              local infos = frame_bottom_model.get_all_branch_infos(param.data.game_id);

              if infos.cur_branch_id ~= param.data.branch_id then
                  frame_bottom_control.switch_branch(param.data.game_id,param.data.branch_id);
              end

              if param.callback_func ~= nil and param.callback_func ~= "" then
                  local rsp_json = base_utility.table_to_json(rsp_table);
                  web_utility.lua_call_js_by_id_ex(param.callback_func, rsp_json, browser_id);
              end
          end
          return
      end
  end;

    get_game_update_pause_status = function (game_id)
        base_utility.log(
            string.format("[frame_bottom_model][get_game_update_pause_status]game_id:%d", game_id));       
        local param = {};
        param["game_id"] = game_id;
        local data_out = comm_center.call_service("SvrGetGameUpdateStatus", param);
        local pause_status = 0;
        local update_progress = 0;
        if nil ~= data_out.pause_status then
            pause_status = data_out.pause_status;
        end
        if nil ~= data_out.update_progress then
            update_progress = data_out.update_progress;
        end

        return pause_status, update_progress;
    end;

    get_game_user_protocol = function (game_id)
        local module_info = banner_model.get_banner_module_info_by_game_id(game_id);
        if nil == module_info or 
            nil == module_info.multi_game_protocol_config or 
            "" == module_info.multi_game_protocol_config then
            return nil;
        end

        local protocol_info = base_utility.json_to_table(module_info.multi_game_protocol_config);

        return protocol_info;
    end;

    --显示礼包
    get_show_download_gift = function(game_id)
        local show_download_gift = frame_bottom_model.get_download_gift_data(game_id, "status");
        if show_download_gift == nil then
            return false;
        end
        return (show_download_gift ~= 0);
    end;

    --显示礼包: 下载礼包数据
    get_download_gift_data = function (game_id, key)
        game_id = game_id or 0;
        if frame_bottom_model.download_gift_data[game_id] == nil then
            local show_download_gift = 0;
            local download_gift_url = "";
            local gift_param = comm_center.call_service("Svr_GetShowDownloadGift", {game_id=game_id});
            if nil ~= gift_param then
                if nil ~= gift_param["show_gift"] then
                    show_download_gift = gift_param["show_gift"];
                end
                if nil ~= gift_param["gift_url"] then
                    download_gift_url = gift_param["gift_url"];
                end
            end
            frame_bottom_model.download_gift_data[game_id] = {status = show_download_gift; url = download_gift_url};
            -- status: 0 不显示，1 要显示，-1 显示过(显示过会把这个标志位置为-1)
        end
        local gift_data = frame_bottom_model.download_gift_data[game_id];
        if key ==nil or key == "" then
            return gift_data;
        else 
            return gift_data[key];
        end;
    end;
    
    get_game_update_mode = function (game_id)
        base_utility.log(
            string.format("[frame_bottom_model][get_game_update_pause_status]game_id:%d", game_id));
        local param = {};
        param["game_id"] = game_id;
        local data_out = comm_center.call_service("i_service_using_tinysetup_update_game", param);
        local use_tinysetup = 0;
        if nil ~= data_out.open then
            use_tinysetup = data_out.open;
        end
        return use_tinysetup;
    end;

    get_game_install_mode = function (game_id)
        base_utility.log("[frame_bottom_model][get_game_intsall_mode]game_id: " .. game_id);
        local param = {};
        param["game_id"] = game_id;
        local data_out = comm_center.call_service("i_service_using_tinysetup_install_game", param);
        local use_tinysetup = 0;
        if nil ~= data_out.open then
            use_tinysetup = data_out.open;
        end
        return use_tinysetup;
    end;

    save_wechat_bind_config = function ()
        local data = {};
        data["common"] = 0;
        data["key"] = "wechat_bind.bind_qq_for_lol";
        local config_tb = {};
        config_tb["is_bind"] = "1";
        data["value"] = base_utility.table_to_json(config_tb);
        comm_center.call_service("Svr_SetConfig", data);
    end;

    get_wechat_bind_config = function ()
        local data = {};
        data["common"] = 0;
        data["key"] = "wechat_bind.bind_qq_for_lol";
        local config = comm_center.call_service("Svr_GetConfig", data);
        return config;
    end;

    query_lol_is_wechat_bind = function ()
        base_utility.log("[frame_bottom_model][query_lol_is_wechat_bind]", true);
        local get_wechat_ctt = comm_center.call_service("Svr_GetWxCTT", {});
        if nil ~= get_wechat_ctt and nil ~= get_wechat_ctt.str_ctt and nil ~= get_wechat_ctt.str_open_id then
            local query_data = {wx_openid = get_wechat_ctt.str_open_id,
                wx_client_tmp_token = get_wechat_ctt.str_ctt, login_type = 1};
            local url = frame_bottom_model.lol_bind_query_url .."?p="..base_utility.table_to_json(query_data);
            base_utility.log("[frame_bottom_model][query_lol_is_wechat_bind]url:" .. url);
            --url = web_utility.make_login_status_url(url, login_model.main_uin);
            base_utility.curl_request_async(url, "frame_bottom_model.on_lol_Is_wechat_bind_result", true, nil, true);
        end
    end;

    on_lol_Is_wechat_bind_result = function (json_str)
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            base_utility.log("[frame_bottom_model][on_lol_Is_wechat_bind_result]result_tb is nil");
            -- 查询失败提示
            frame_bottom_model.show_lol_bind_query_fail_tips();
            return;
        end

        if 0 == result_tb.result then
            if false == result_tb.is_bind_main_account then
                -- 未绑定，弹框
                wechat_bind_view.ShowNoBindView();
            else
                -- 绑定，则继续启动流程
                state_operation_panel.StartGameHelper(frame_bottom_model.lol_game_id, frame_bottom_model.lol_service_id);
            end
        else
            -- 查询失败提示
            frame_bottom_model.show_lol_bind_query_fail_tips();
        end
    end;

    is_lol_bind_switch_open = function ()
        local is_open = true;
        local req_data = {key = "lol_wechat_bind_switch"};
        local data = comm_center.call_service("i_svr_get_common_cfg", req_data);
        if nil ~= data and data.value ~= nil and data.value ~= "" then
            local data_tb = base_utility.json_to_table(data.value);
            if nil ~= data_tb and false == data_tb.switch_open  then
                is_open = false;
            end
        else
            TGPLog.LogE("is_lol_bind_switch_open", "query fail.")
        end;
        return is_open;
    end;

    show_lol_bind_query_fail_tips = function ()
        local param = {
            position = "center",   -- "rbottom": 右下角，"center": 居中
            title = LoadStr("POP_WND_REPAIR_TITLE"),
            width = 480,
            height = 230,
            titleIcon = "",
            icon = "",
            tipsIcon = poptips_icons.kError,
            descTitle = "",
            desc = LoadStr("POP_WND_LOL_BIND_QUERY_FAIL_DESC"),
            okBtn = {
                visible = "true",
                close_win = true,
                text = LoadStr("POP_WND_QUERYGAMEFAILED_OK"),
                handle = function()
                end,
            },
            cancelBtn = {
                visible = "false",
                text = LoadStr("POP_WND_REPAIR_CANCEL"),
                handle = function()
                end,
            },
       }
       ui_utility.show_tips(param);
    end;

    is_all_protocol_agree = function (game_id)
        local info = frame_bottom_model.get_game_user_protocol(game_id);
        if nil == info then
            return true;
        end
        if tonumber(info.show_protocol) == 0 then
            return true;
        end

        local is_agree = true;
        if nil == frame_bottom_model.is_user_protocol_agree_table or 
            type(frame_bottom_model.is_user_protocol_agree_table) ~= "table" or
            nil == frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)] then
            -- 读默认的配置
            if nil ~= info and nil ~= info.protocol_list then
                for k, v in pairs(info.protocol_list) do
                    if false == v.is_check or nil == v.is_check then
                        is_agree = false;
                        break;
                    end
                end
            end
        else
            local agree_info = frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)];
            if nil ~= agree_info and nil ~= info and nil ~= info.protocol_list 
                and type(agree_info) == "table" then
                for k, v in pairs(info.protocol_list) do
                    base_utility.log("[frame_bottom_model][is_all_protocol_agree]"..v.id..tostring(agree_info[v.id]));
                   if false == agree_info[v.id] or nil == agree_info[v.id] then
                      is_agree = false;
                     break;
                   end
                end
            end            
        end
        return is_agree;
    end;

    get_game_protocol_agree_info = function ()
        local data = comm_center.call_service("i_service_get_my_setting", {key = "game_protocol_agree_tb"});
        if nil ~= data and nil ~= data.value then
            frame_bottom_model.is_user_protocol_agree_table = base_utility.json_to_table(data.value);
        end
        -- 云端取不到，则使用本地的
        if nil == frame_bottom_model.is_user_protocol_agree_table then
            local protocol_info_req = {
                common = 0, key = "frame_bottom.game_protocol_info"};
            local protocol_info = comm_center.call_service("Svr_GetConfig", protocol_info_req);
            if 0 ~= protocol_info.exits and nil ~= protocol_info.value then
                frame_bottom_model.is_user_protocol_agree_table = base_utility.json_to_table(protocol_info.value);
            end
        end
    end;

    is_protocol_check = function (game_id, protocol_id)
        local is_check = true;
        if nil ~= frame_bottom_model.is_user_protocol_agree_table and
            type(frame_bottom_model.is_user_protocol_agree_table) == "table" and
            nil ~= frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)] and
            nil ~= frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)][protocol_id] then
            local info = frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)];
            is_check = info[protocol_id];
        else
            local protocol_info = frame_bottom_model.get_game_user_protocol(game_id);
            if nil ~= protocol_info and nil ~= protocol_info.protocol_list then
                for k, v in pairs(protocol_info.protocol_list) do
                    if v.id == protocol_id then
                        is_check = v.is_check;
                        break;
                    end
                end
            end
        end
        return is_check;
    end;

    clear_protocol_agree_by_gameid = function(game_id)
        base_utility.log("[frame_bottom_model][clear_protocol_agree_by_gameid]" ..
                tostring(game_id));
        local protocol_info = frame_bottom_model.get_game_user_protocol(game_id);
        if nil ~= protocol_info and nil ~= protocol_info.protocol_list then
            local check_tb = {};
            for k, v in pairs(protocol_info.protocol_list) do
                check_tb[v.id] = false;
            end
            frame_bottom_model.update_protocol_agree_table_by_gameid(game_id, check_tb);
        else
            base_utility.log("[frame_bottom_model][clear_protocol_agree_by_gameid]" ..
                    tostring(game_id) .. ", protocollist nil", true);
        end


    end;
    update_protocol_agree_table_by_gameid = function(game_id, info)
        if nil == frame_bottom_model.is_user_protocol_agree_table then
            frame_bottom_model.is_user_protocol_agree_table = {};
        end
        frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)] = info;
        frame_bottom_model.save_protocol_agree_tb();
    end;

    update_protocol_agree_table = function (game_id, protocol_id, value)
        if nil == frame_bottom_model.is_user_protocol_agree_table then
            frame_bottom_model.is_user_protocol_agree_table = {};
            local check_tb = {};
            check_tb[protocol_id] = value;
            frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)] = check_tb;
        elseif type(frame_bottom_model.is_user_protocol_agree_table) == "table" then
            local agree_info = frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)];
            if nil ~= agree_info then
                agree_info[protocol_id] = value;
            else
                agree_info = {};
                agree_info[protocol_id] = value;
                frame_bottom_model.is_user_protocol_agree_table[tostring(game_id)] = agree_info;
            end
        end
        frame_bottom_model.save_protocol_agree_tb();
    end;

    save_protocol_agree_tb = function ()
        if nil == frame_bottom_model.is_user_protocol_agree_table then
            return;
        end
        
        if type(frame_bottom_model.is_user_protocol_agree_table) ~= "table" then
            base_utility.log("[frame_bottom_model][save_protocol_agree_tb]not table");
          frame_bottom_model.is_user_protocol_agree_table = {};
        end
        local agree_tb_js = base_utility.table_to_json(frame_bottom_model.is_user_protocol_agree_table);
        -- 本地记录
        local protocol_info_req = {
            common = 0, key = "frame_bottom.game_protocol_info", value = agree_tb_js};
        comm_center.call_service("Svr_SetConfig", protocol_info_req);
        -- 云端存储
        local all_setting_rsp = comm_center.call_service("i_service_get_all_setting", {});
        if nil ~= all_setting_rsp then
            local all_setting_tb = {};
            if nil ~= all_setting_rsp.all_setting then
                all_setting_tb = all_setting_rsp.all_setting;
            end
            all_setting_tb["game_protocol_agree_tb"] = agree_tb_js;
            local all_setting_js = base_utility.table_to_json(all_setting_tb);
            local update_config_url = LoadStr("UPDATE_USER_CONFIG_URL") .. "GameID=55555&Key=UserSet&Len=";
            local url = update_config_url .. string.len(all_setting_js) .. "&Value=" .. all_setting_js; 
            --url = web_utility.make_login_status_url(url, wegame_mainframe_model.self_uin);
            base_utility.curl_request_async (url,
                "frame_bottom_model.handle_update_user_setting", true, nil, true);
        end
    end;

    handle_update_user_setting = function (json_str)
        local result_tb = base_utility.json_to_table(json_str);
        if nil == result_tb then
            return;
        end
        if result_tb.Ret ~= nil and result_tb.Ret == 0 then
            comm_center.send_broadcast("i_broadcast_game_protocol_update_suc", {});
        end;
        base_utility.log(string.format("[frame_bottom_model][handle_update_user_setting]json_str:%s", json_str), true);
    end;

    on_user_setting_update = function()
        frame_bottom_model.get_game_protocol_agree_info();
        comm_center.send_broadcast("i_broadcast_game_protocol_update_suc", {});
    end;

    reset_frame_bottom_style_status = function()
        frame_bottom_model.show_new_frame_bottom_ = false;
    end;
};

frame_bottom_model.init();

